package mylist
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.ActivityEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.StatusEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class MyCam extends Sprite
	{
		
		
		
		public static var main:MyCam;
		static public var  OUTPUT_WIDTH:int = 768;
		static public var  OUTPUT_HEIGHT:int = 1024;
		static public const FLV_FRAMERATE:int = 10;
		public static var video:Video;
		private static var camera:Camera;
		
		public function MyCam()
		{
			if (main)
				return;
			main = this;
			mouseChildren = false;
			
			
			addEventListener(Event.ADDED_TO_STAGE, PhotoSnap);
		}
		
		private static var timeOutId:uint;
		
		public function startCam(e:Event=null):void
		{
			stage.removeEventListener(MouseEvent.CLICK, savepic);
			//logs.adds("startCam");
			if (video)
				video.visible = true;
			clearTimeout(timeOutId);
			timeOutId = setTimeout(canClick, 100);
		}
		
		private function canClick():void
		{
			clearTimeout(timeOutId);
			stage.addEventListener(MouseEvent.CLICK, savepic);
		}
		
		
		
		private function PhotoSnap(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, PhotoSnap);
			OUTPUT_HEIGHT = stage.stageHeight-100;
			OUTPUT_WIDTH = stage.stageWidth;
			
			camera = Camera.getCamera();
			if (camera == null)
			{
				stage.addChild(new logs);
				logs.adds("no camera!");
			}else {
				//logs.adds("camera getted!");
			}
			if (camera == null) return;
			camera.setMode(OUTPUT_WIDTH, OUTPUT_HEIGHT, FLV_FRAMERATE);
			camera.setQuality(0, 100);
			video = new Video();
			video.y = 50;
			//video = new Video(OUTPUT_WIDTH, OUTPUT_HEIGHT);
			video.width = OUTPUT_WIDTH;
			video.height = OUTPUT_HEIGHT;
			video.attachCamera(camera);
			addChild(video);
			video.visible = false;
		}
		
		public function savepic(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.CLICK, savepic);
			if (camera == null) return;
			var jpgSource:BitmapData = new BitmapData(camera.width, camera.height);
			var myMatrix:Matrix = new Matrix();
			myMatrix.scale(camera.width/(video.width/video.scaleX), camera.height/(video.height/video.scaleY)); //关键处，该处的值应该是由video的大小和camera设置的大小决定的。
			jpgSource.draw(video, myMatrix);
			var file:FileReference = new FileReference();
			var jpg:JPGEncoder = new JPGEncoder(100);
			
			ViewList.main.addChilds(new Bitmap(jpgSource));
			video.visible = false;
			//file.save(jpg.encode(jpgSource), "b.jpg");
		}
		
		private function saveData(e:Event = null):void
		{
			stage.removeEventListener(MouseEvent.CLICK, saveData);
			if (camera == null)
				return;
			//logs.adds("saveData");
			var bmpd:BitmapData = new BitmapData(video.width / video.scaleX, video.height / video.scaleY, false, 0xcccccccc);
			//var rect:Rectangle = new Rectangle(0, 0, OUTPUT_HEIGHT, OUTPUT_WIDTH);
			//var matrix:Matrix = new Matrix(1, 0, 0, 1, 0, 0);
			//matrix.rotate(-Math.PI / 2);
			//matrix.ty = OUTPUT_WIDTH;
			//bmpd.draw(video, matrix, null, null, rect);
			bmpd.draw(video);
			ViewList.main.addChilds(new Bitmap(bmpd));
			
			video.visible = false;
		}
	
	}

}